package com.action;

import com.actions.AbstractBaseAction;
import com.service.IStudentService;
import com.vo.Student;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/student/*")
@Slf4j
public class StudentAction extends AbstractBaseAction {

    @Autowired
    private JdbcTemplate jdbcTemplate;



    @RequestMapping("list1")
    public Object list1() {
        String sql = "SELECT id, studentnumber, name, age, birthday, college, classnum FROM t_student";
        List<Student> allStudents = this.jdbcTemplate.query(sql,
                new RowMapper<Student>() {
                    @Override
                    public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
                        Student student = new Student();
                        student.setId(rs.getString(1));
                        student.setStudentnumber(rs.getString(2));
                        student.setName(rs.getString(3));
                        student.setAge(rs.getInt(4));
                        student.setBirthday(rs.getDate(5));
                        student.setCollege(rs.getString(6));
                        student.setClassnum(rs.getString(7));
                        return student;
                    }
                });
        return allStudents;
    }

    //localhost:8088/member/list1
    //localhost:8088/druid


    //11.2整合mybatisplus部分
    @Autowired
    private IStudentService studentService;

    @RequestMapping("list")
    public Object list() {

        //log.info("查询全部Member数据。");
        return this.studentService.list();
    }
    @RequestMapping("get")
    public Object get(String id) {
        //log.info("查询用户数据：{}", mid);
        return this.studentService.get(id);
    }


    @RequestMapping("add")
    public Object add(Student member) {
        //log.info("增加新的用户数据：{}", member);
        return this.studentService.add(member);
    }
    @RequestMapping("delete")
    public Object delete(String ... id) {
        //log.info("根据ID删除数据：{}", id);
        Set<String> ids = new HashSet<>();
        ids.addAll(Arrays.asList(id));
        return this.studentService.delete(ids);
    }
    @RequestMapping("split")
    public Object split(String column, String keyword, int currentPage, int lineSize) {
        //  log.info("数据分页显示，查询模糊列：{}、查询关键字：{}、当前页：{}、每页行数：{}");
        return this.studentService.listSplit(column, keyword, currentPage, lineSize);
    }
    @RequestMapping("getStudentnumber")
    public Object getStudentnumber(String studentnumber) {

        return this.studentService.getStudentnumber(studentnumber);
    }
    @RequestMapping("deleteStudentnumber")
    public boolean deleteStudentnumber(String studentnumber) {
        return this.studentService.deleteStudentnumber(studentnumber);
    }

    //localhost:8088/member/list
    //localhost:8088/member/get?mid=yootk
    //localhost:8088/member/add/?mid=liusong&keyword=221009&name=刘松&age=34

}